﻿/*************************   Auto Generated Repository Code For EncounterVital table     Generated @ 5/7/2013 9:13:05 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class EncounterVitalRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an EncounterVital by its EncounterVitalID (Primary Key)
        /// </summary>
        /// <param name="EncounterVitalID">The ID of the <see cref="EncounterVital"/> to retrieve</param>
        /// <returns>A <see cref="EncounterVital"/> object</returns>
        public BusinessObjects.EncounterVital Select(int EncounterVitalID)
        {
            EncounterVital retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.EncounterVitals where x.EncounterVitalID == EncounterVitalID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of EncounterVital objects
        /// </summary>
        /// <returns>A List of type <see cref="EncounterVital"/></returns>
        public List<BusinessObjects.EncounterVital> SelectAll()
        {
            List<BusinessObjects.EncounterVital> retObject = new List<BusinessObjects.EncounterVital>();

            using (dB = new PracticeSimpleDataContext())
            {
                var EncounterVitalList = (from x in dB.EncounterVitals select x).ToList();

                foreach (EncounterVital obj in EncounterVitalList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }

        /// <summary>
        /// Return a list of EncounterVital objects
        /// </summary>
        /// <param name="EncounterID">The id of the encounter</param>
        /// <returns>A List of type <see cref="EncounterVital"/></returns>      
        public List<BusinessObjects.EncounterVital> GetVitalsForEncounter(int EncounterID)
        {
            List<BusinessObjects.EncounterVital> retObject = new List<BusinessObjects.EncounterVital>();

            using (dB = new PracticeSimpleDataContext())
            {
                var EncounterVitalList = (from x in dB.EncounterVitals where x.EncounterID == EncounterID select new { x.EncounterVitalID, x.VitalID, x.Vital.VitalName, x.VitalValue, x.CreatedDate, x.Vital.ParentVitalID }).ToList();

                foreach (var dbEv in EncounterVitalList)
                {
                    BusinessObjects.EncounterVital ev = new BusinessObjects.EncounterVital();

                    switch (dbEv.ParentVitalID)
                    {
                        case 1: //height
                            ev.VitalName = "Height";
                            string feet = EncounterVitalList.Where(x => x.VitalID == 2).Select(x => x.VitalValue).SingleOrDefault();
                            string inches = EncounterVitalList.Where(x => x.VitalID == 3).Select(x => x.VitalValue).SingleOrDefault();
                            ev.VitalValue = String.Format("{0}' {1}\"", feet, inches);
                            ev.CreatedDate = dbEv.CreatedDate;
                        break;

                        case 5: //BP
                            ev.VitalName = "BP";
                            string sys = EncounterVitalList.Where(x => x.VitalID == 6).Select(x => x.VitalValue).SingleOrDefault();
                            string dia = EncounterVitalList.Where(x => x.VitalID == 7).Select(x => x.VitalValue).SingleOrDefault();
                            ev.VitalValue = String.Format("{0}/{1}", sys, dia);
                            ev.CreatedDate = dbEv.CreatedDate;
                        break;
                        
                        default:
                        ev.EncounterVitalID = dbEv.EncounterVitalID;
                        ev.VitalName = dbEv.VitalName;
                        ev.VitalValue = dbEv.VitalValue;
                        ev.CreatedDate = dbEv.CreatedDate;
                        break;
                    }

                    if (retObject.Where(x => x.VitalName == ev.VitalName).Count() < 1)
                    {
                        retObject.Add(ev);
                    }
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a EncounterVital Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public BusinessObjects.EncounterVital Save(BusinessObjects.EncounterVital businessObject)
        {
            EncounterVital dbEncounterVital;
            using (dB = new PracticeSimpleDataContext())
            {
                dbEncounterVital = ConvertToDataObject(businessObject);
                dB.EncounterVitals.InsertOnSubmit(dbEncounterVital);
                dB.SubmitChanges();
            }

            return ConvertToBusinessObject(dbEncounterVital);
        }

        /// <summary>
        /// Accepts the Primary Key of a EncounterVital, finds it in the database and deletes it
        /// </summary>
        /// <param name="EncounterVitalID">The ID of the <see cref="EncounterVital"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int EncounterVitalID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.EncounterVital obj = (from x in dB.EncounterVitals where x.EncounterVitalID == EncounterVitalID select x).SingleOrDefault();
                dB.EncounterVitals.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a EncounterVital data object to a EncounterVital business object
        /// </summary>
        /// <returns>A business object of type <see cref="EncounterVital"/></returns>
        public BusinessObjects.EncounterVital ConvertToBusinessObject(DataLayer.EncounterVital dataObject)
        {
            BusinessObjects.EncounterVital businessObject = new BusinessObjects.EncounterVital();

            businessObject.EncounterVitalID = dataObject.EncounterVitalID;
            businessObject.EncounterID = dataObject.EncounterID;
            businessObject.VitalID = dataObject.VitalID;
            businessObject.VitalValue = dataObject.VitalValue;
            businessObject.CreatedDate = dataObject.CreatedDate;
            businessObject.CreatedBy = dataObject.CreatedBy;


            return businessObject;

        }

        /// <summary>
        /// Converts a EncounterVital business object to a EncounterVital data object
        /// </summary>
        /// <returns>A data object of type <see cref="EncounterVital"/></returns>
        public DataLayer.EncounterVital ConvertToDataObject(BusinessObjects.EncounterVital businessObject)
        {
            DataLayer.EncounterVital dataObject = new DataLayer.EncounterVital();

            dataObject.EncounterVitalID = businessObject.EncounterVitalID;
            dataObject.EncounterID = businessObject.EncounterID;
            dataObject.VitalID = businessObject.VitalID;
            dataObject.VitalValue = businessObject.VitalValue;
            dataObject.CreatedDate = businessObject.CreatedDate;
            dataObject.CreatedBy = businessObject.CreatedBy;


            return dataObject;
        }
    }
}


